package com.tiancikeji.zaoke.app.service.user.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.tiancikeji.zaoke.app.dao.code.CodeDao;
import com.tiancikeji.zaoke.app.dao.user.UserDao;
import com.tiancikeji.zaoke.app.model.code.Code;
import com.tiancikeji.zaoke.app.model.user.User;
import com.tiancikeji.zaoke.app.service.user.UserService;
import com.tiancikeji.zaoke.app.util.ZaokeUtils;

public class UserServiceImpl implements UserService {
	
	private UserDao userDao;
	
	private CodeDao codeDao;
	
	private long expireTime = 300;
	
	private int codeLenth = 4;

	public User getUserInfo(long userid){
		return userDao.getUserInfo(userid);
	}

	public User autoRegisterUser(String name){
		if(StringUtils.isNotBlank(name)){
			User user = new User();
			user.setUserName(name);
			user.setState(User.STATE_AUTO);
			Date now = new Date();
			user.setRegisterTime(now);
			user.setModTime(now);
			
			long userid = userDao.addAutoUser(user);
			if(userid > 0){
				user.setUserId(userid);
				return user;
			}
		}
		return null;
	}

	public String addVerifyCode(String phone, User user) {
		if(user != null && user.getUserId() > 0){
			Code code = codeDao.getCodeInfo(user.getUserId(), System.currentTimeMillis() / 1000 - expireTime);
			if(code == null){
				String codeStr = ZaokeUtils.getRandInt(codeLenth);
				if(StringUtils.isNotBlank(codeStr)){
					code = new Code();
					code.setCellPhone(phone);
					code.setCode(codeStr);
					code.setUserId(user.getUserId());
					code.setAddTime(System.currentTimeMillis() / 1000);
					
					code.setId(codeDao.addCode(code));
				}
			}
			if(code != null && code.getId() > 0)
				return code.getCode();
		}
		return StringUtils.EMPTY;
	}

	public String getVerifyCode(String phone, User user) {
		if(user != null && user.getUserId() > 0){
			Code code = codeDao.getCodeInfo(user.getUserId(), System.currentTimeMillis() / 1000 - expireTime);
			if(code != null)
				return code.getCode();
		}
		return StringUtils.EMPTY;
	}

	public boolean updateCardId(String cId,  Double money, User user) {
		return userDao.updateCardIdByUserId(cId, money, user.getUserId());
	}

	public User updateUserPhoneNPassword(String password, String phone, User user) {
		if(user != null){
			user.setPassword(StringUtils.trimToEmpty(password));
			user.setCellPhone(StringUtils.trimToEmpty(phone));
			user.setState(User.STATE_MAN);
			if(userDao.updateUserPhoneNPassword(user))
				return user;
		}
		return null;
	}

	public boolean checkPhone(String phone) {
		if(StringUtils.isNotBlank(phone))
			return userDao.getPhoneCount(StringUtils.trimToEmpty(phone), new Object[]{User.STATE_MAN, User.STATE_LOCK}) > 0;
		else
			return false;
	}

	public User getUserInfo(String phone, String password) {
		if(StringUtils.isNotBlank(phone) && StringUtils.isNotBlank(password)){
			List<User> users = userDao.getUsersByPhone(StringUtils.trimToEmpty(phone), StringUtils.trimToEmpty(password), new Object[]{User.STATE_MAN});
			if(users != null && users.size() > 0)
				return users.get(0);
		}
		return null;
	}

	public boolean updatePassword(String password, User user) {
		return userDao.updatePassword(password, user);
	}

	public void updateUserOrder(long userId, Date orderTime, long zoneId, int payway) {
		userDao.updateOrderTimeNZoneIdByUserId(userId, orderTime, zoneId, payway);
	}

	public boolean rename(String name, User user) {
		return userDao.rename(name, user);
	}

	public boolean addBalance(long userId, double money, double balance) {
		return userDao.updateMoney(userId, money, balance);
	}

	public boolean addBalance(long userId, double money) {
		return userDao.updateMoney(userId, money);
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public CodeDao getCodeDao() {
		return codeDao;
	}

	public void setCodeDao(CodeDao codeDao) {
		this.codeDao = codeDao;
	}

	public long getExpireTime() {
		return expireTime;
	}

	public void setExpireTime(long expireTime) {
		this.expireTime = expireTime;
	}

	public int getCodeLenth() {
		return codeLenth;
	}

	public void setCodeLenth(int codeLenth) {
		this.codeLenth = codeLenth;
	}
}
